/**
 * 
 */
package com.ctg.payroll2.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.ctg.payroll2.util.Constants;
import com.ctg.payroll2.util.PayrollUtil;
import com.ctg.platform.util.Page;
import com.ctg.platform.util.Parse;
import com.ctg.tax.bean.po.TaxTable;
import com.ctg.tax.service.TaxTableService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author 郑致力
 *
 */
public class TaxAction extends ActionSupport {
	private Page result;
	private List<TaxTable> resultL;
	
	public String queryTax() {
		Integer code = null;
		String queryKey = null;
		Integer start = null;
		Integer limit = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		code = Parse.toInteger(request.getParameter("code")); 
		queryKey = request.getParameter("queryKey");
		start = Parse.toInteger(request.getParameter("start") , 0);
		limit = Parse.toInteger(request.getParameter("limit"), Constants.DEFAULT_PAGE_SIZE);
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		TaxTableService taxTableService = (TaxTableService) wac.getBean("taxTableService");
		result = taxTableService.queryWithPagination(code, queryKey, true, false, start, limit);
		return SUCCESS;
	}
	
	public String queryTaxDetails() {
		Integer code = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		code = Parse.toInteger(request.getParameter("code"));
		
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		TaxTableService taxTableService = (TaxTableService) wac.getBean("taxTableService");
		resultL = taxTableService.query(code, null, true, true);
		return SUCCESS;		
	}
	
	public String saveTax() {
		String tax = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		tax = request.getParameter("tax");
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		TaxTableService taxTableService = (TaxTableService) wac.getBean("taxTableService");
		taxTableService.insertUpdateByJson(tax);
		return SUCCESS;
	}
	
	public String delTax() {
		String ids = null;
		int [] _ids = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		ids = Parse.toString((request.getParameter("tax")) , false);
		if (ids != null) {
//			System.out.println(ids);
			_ids = PayrollUtil.splitStringToIntArray(ids);
			WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
			TaxTableService taxTableService = (TaxTableService) wac.getBean("taxTableService");
			for (int i = 0 ; i < _ids.length ; i++) {
//				System.out.println( 
						taxTableService.safeDelete(_ids[i]);
//						);
			}
		} else {
			return SUCCESS;
		}
		return SUCCESS;
	}

	public Page getResult() {
		return result;
	}

	public void setResult(Page result) {
		this.result = result;
	}

	public List<TaxTable> getResultL() {
		return resultL;
	}

	public void setResultL(List<TaxTable> resultL) {
		this.resultL = resultL;
	}
	
}
